/**
 * 回车监听
 */
define('editorenhancer/EnterHandler', null, () => {

var XEditor = window.XEditor;

function EnterHandler(editor, options) {
    this.editor = editor;
    this.options = options;
    this.lineMode = editor.configs.lineMode;

    editor.on('keyup', this.handler, this);
}
EnterHandler.prototype = {
    constructor: EnterHandler,
    handler: function(e) {
        if(13 !== e.keyCode) {
            return;
        }

        let range = XEditor.Editable.getCurrentRange();
        if(!range) {
            return;
        }

        let outer = range.getOutermostElement();
        let name = outer.nodeName.toLowerCase();

        if(name === this.lineMode.toLowerCase()) {
            return;
        }

        let newNode = this.editor.doc.createElement(this.lineMode);
        newNode.innerHTML = '<br />';

        if('blockquote' === name) {
            let secondLevelNode = range.pathInfo()[1];
            if(secondLevelNode && secondLevelNode.nodeName.toLowerCase() !== this.lineMode.toLowerCase()) {
                outer.replaceChild(newNode, secondLevelNode);
                XEditor.Editable.resetRangeAt(newNode, true);
            }

            return;
        }

        if('div' === name) {
            outer.parentNode.replaceChild(newNode, outer);

            XEditor.Editable.resetRangeAt(newNode, true);
        }
    }
};

return EnterHandler;

});

